package com.sykj.why.controller;

import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.sykj.why.exception.WhyException;
import com.sykj.why.handler.Response;
import com.sykj.why.mongo.document.entity.MenuCategoryT;
import com.sykj.why.mongo.document.entity.SystemMenuT;
import com.sykj.why.mongo.enums.exception.ResultEnum;
import com.sykj.why.mongo.service.MenuCategoryTService;
import com.sykj.why.mongo.service.SystemMenuTService;
import com.sykj.why.mongo.service.impl.MenuCategoryTServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Scanner;

/**
 * @author Eleven
 * @date 2019/6/6
 * @desc 测试接口
 */

@RestController
@RequestMapping("/test")
public class TestController {

    @Autowired
    private SystemMenuTService systemMenuTService;
    @Autowired
    private MenuCategoryTService menuCategoryTService;

    @RequestMapping(value = "/getOneSystemMenu",method = RequestMethod.GET)
    public ResponseEntity getOneSystemMenu(){
        try {
            List<SystemMenuT> systemMenuList = systemMenuTService.getSystemMenuListByPid("1");
            return Response.success("获取一级系统菜单成功",systemMenuList);
        } catch (WhyException e) {
            return Response.serverError(e.getMessage(), e.getCode());
        } catch (Exception e) {
            System.out.println("Test-获取一级系统菜单成功出现未知异常,"+e.getMessage());
            return Response.serverError(ResultEnum.UNERROR.getMessage(), ResultEnum.UNERROR.getCode());
        }
    }

    /**
     * 加载菜单
     * @param session
     * @return
     * @throws Exception
     * 这里传入的pid是1
     */
    @ResponseBody
    @GetMapping("/loadTwoMenuInfo")
    public String loadMenuInfo(HttpSession session, String pid)throws Exception{
        //根据当前用户的角色id和父节点id查询所有菜单及子集json
        String json=getAllMenuByParentId(pid).toString();
        //System.out.println(json);
        return json;
    }

    /**
     * 获取根频道所有菜单信息
     * @param pid
     * @return
     */
    private JsonObject getAllMenuByParentId(String pid){
        JsonObject resultObject=new JsonObject();
        JsonArray jsonArray=this.getMenuByParentId(pid);//得到所有一级菜单
        for(int i=0;i<jsonArray.size();i++){
            JsonObject jsonObject=(JsonObject) jsonArray.get(i);
            //判断该节点下是否还有子节点
            List<SystemMenuT> menuList=systemMenuTService.getSystemMenuListByPid(pid);
            //if("true".equals(jsonObject.get("spread").getAsString())){
            if (menuList.size()==0) {
                continue;
            }else{
                //由于后台模板的规定，一级菜单以title最为json的key
                resultObject.add(jsonObject.get("title").getAsString(), getAllMenuJsonArrayByParentId(jsonObject.get("id").getAsString()));
            }
        }
        return resultObject;
    }

    //获取根频道下子频道菜单列表集合
    private JsonArray getAllMenuJsonArrayByParentId(String pid){
        JsonArray jsonArray=this.getMenuByParentId(pid);
        for(int i=0;i<jsonArray.size();i++){
            JsonObject jsonObject=(JsonObject) jsonArray.get(i);
            //判断该节点下是否还有子节点
            List<SystemMenuT> menuList=systemMenuTService.getSystemMenuListByPid(pid);
            //if("true".equals(jsonObject.get("spread").getAsString())){
            if (menuList.size()==0) {
                continue;
            }else{
                //二级或三级菜单
                jsonObject.add("children", getAllMenuJsonArrayByParentId(jsonObject.get("id").getAsString()));
            }
        }
        return jsonArray;
    }

    /**
     * 根据父节点查询菜单
     * @param pid
     * @return
     */
    private JsonArray getMenuByParentId(String pid){
        List<SystemMenuT> menuList=systemMenuTService.getSystemMenuListByPid(pid);
        JsonArray jsonArray=new JsonArray();
        for(SystemMenuT menu:menuList){
            JsonObject jsonObject=new JsonObject();
            jsonObject.addProperty("id", menu.getId()); // 节点id
            jsonObject.addProperty("title", menu.getName()); // 节点名称
            jsonObject.addProperty("spread", false); // 不展开
            jsonObject.addProperty("icon", menu.getIcon());
            if(StringUtils.isNotEmpty(menu.getUrl())){
                jsonObject.addProperty("href", menu.getUrl()); // 菜单请求地址
            }
            jsonArray.add(jsonObject);

        }
        return jsonArray;
    }

    @RequestMapping(value = "/getFathers/{id}",method = RequestMethod.GET)
    public ResponseEntity getFathers(@PathVariable String id){
        try {
            List<MenuCategoryT> menuCategoryList = menuCategoryTService.findFathers(id);
            return Response.success("获取菜单分类父级菜单成功",menuCategoryList);
        } catch (WhyException e) {
            return Response.serverError(e.getMessage(), e.getCode());
        } catch (Exception e) {
            System.out.println("Test-获取菜单分类父级菜单成功出现未知异常,"+e.getMessage());
            return Response.serverError(ResultEnum.UNERROR.getMessage(), ResultEnum.UNERROR.getCode());
        }
    }

    @RequestMapping(value = "/getCategoryAllName/{id}",method = RequestMethod.GET)
    public String getCategoryAllName(@PathVariable String id){
        //分类全称
        List<MenuCategoryT> fathers= menuCategoryTService.findFathers(id);
        String categoryAllName = "";
        if (fathers!=null){
            for (MenuCategoryT menuCategory : fathers){
                categoryAllName += menuCategory.getCategoryName() + "-";
            }
        }
        return categoryAllName.substring(0,categoryAllName.length()-1);
    }

}
